{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_defaultSpec_1599037330542",
   "display_name": "Python 3.8.5 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "给定一个表示分数的非负整数数组。 玩家 1 从数组任意一端拿取一个分数，随后玩家 2 继续从剩余数组任意一端拿取分数，然后玩家 1 拿，…… 。每次一个玩家只能拿取一个分数，分数被拿取之后不再可取。直到没有剩余分数可取时游戏结束。最终获得分数总和最多的玩家获胜。\n",
    "\n",
    "给定一个表示分数的数组，预测玩家1是否会成为赢家。你可以假设每个玩家的玩法都会使他的分数最大化。\n",
    "\n",
    " \n",
    "\n",
    "示例 1：\n",
    "```\n",
    "输入：[1, 5, 2]\n",
    "输出：False\n",
    "```\n",
    "解释：一开始，玩家1可以从1和2中进行选择。\n",
    "如果他选择 2（或者 1 ），那么玩家 2 可以从 1（或者 2 ）和 5 中进行选择。如果玩家 2 选择了 5 ，那么玩家 1 则只剩下 1（或者 2 ）可选。\n",
    "所以，玩家 1 的最终分数为 1 + 2 = 3，而玩家 2 为 5 。\n",
    "因此，玩家 1 永远不会成为赢家，返回 False 。\n",
    "\n",
    "来源：力扣（LeetCode）\n",
    "链接：https://leetcode-cn.com/problems/predict-the-winner\n",
    "著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "思路：\n",
    "\n",
    "一点头绪也没有。。。、\n",
    "\n",
    "看了一下评论，发现大家说偶素长度的数组，先选的必然获胜\n",
    "\n",
    "奇数的长度数组，可以用dp来解决，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 看了一个非官方的题解，比较好理解\n",
    "\n",
    "# def printx()\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def PredictTheWinner(self, nums: List[int]) -> bool:\n",
    "        \"\"\"start, end 表示当前可选的数组是从start到end\n",
    "        turn\n",
    "        \"\"\"\n",
    "        def helper(start, end):\n",
    "            if start == end:\n",
    "                return nums[start]\n",
    "            # 当1选了一个的时候，2会有两种选择，2选了一个数的时候，1也会有两种选择\n",
    "            # print(start, end)\n",
    "            # 选了前面可以超过2号玩家的多少分\n",
    "            pick_start = nums[start] - helper(start+1, end)\n",
    "\n",
    "            # 选了后面可以超过2号玩家的多少分\n",
    "            pick_end = nums[end] - helper(start, end-1)\n",
    "            print(pick_start, pick_end, start, end)\n",
    "            return max(pick_start, pick_end)\n",
    "        \n",
    "        return helper(0, len(nums)-1) >= 0\n",
    "            "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以 `[1,10,8]`举例\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "-4 4 0 1\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "True"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "Solution().PredictTheWinner([1, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "-2 2 1 2\n-4 4 0 1\n-1 3 0 2\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "True"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "Solution().PredictTheWinner([1, 5, 7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "0 3\n1 3\n2 3\n1 2\n0 2\n1 2\n0 1\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "True"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "Solution().PredictTheWinner([1, 5, 233, 7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}